home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / compress / tar321__.zip / SOURCES.ZIP / FMATCH.C < prev    next >
C/C++ Source or Header  |  1994-12-31  |  2KB  |  98 lines

  1. #include "modern.h"
  2. #ifdef MODERN
  3. #    include <string.h>
  4. #else
  5.     int  strlen();
  6. #endif
  7. #define ERROR (-1)
  8. #define FALSE 0
  9. #define TRUE  1
  10.  
  11. #ifdef MSDOS
  12. static int fnmatch __ARGS__((char*, char*, int));
  13.  
  14. static int fnmatch(p, s, l)
  15. register char *p; /* pattern */
  16. register char *s; /* name to compare */
  17. {
  18.    register c1, c2;
  19.  
  20.    for (; *p && l; ++p, ++s, l--) {
  21.       if (*p == '*') {
  22.          while (*++p == '*');
  23.          while (l) if (fnmatch(p, s++, l--)) return TRUE;
  24.          break;
  25.       }
  26.       if (*p == '?') continue;
  27.  
  28.       if ((c1 = *p) >= 'a' && c1 <= 'z') c1 -= 'z'-'Z';
  29.       else if (c1 == '\\') c1 = '/';
  30.  
  31.       if ((c2 = *s) >= 'a' && c2 <= 'z') c2 -= 'z'-'Z';
  32.       else if (c2 == '\\') c2 = '/';
  33.  
  34.       if (c1 != c2) return FALSE;
  35.    }
  36.    return !(*p || l);
  37. }
  38.  
  39. int fmatch(p, s)
  40. register char *p; /* pattern */
  41. register char *s; /* name to compare */
  42. {
  43.    register i, j;
  44.  
  45.    if (fnmatch(p, s, (i=strlen(s)))) return TRUE;
  46.    if (!i) return FALSE;
  47.  
  48.    j = i;
  49.    do {
  50.       if (s[--j] == '.') goto dot;
  51.    } while (j && s[j]!='\\' && s[j]!='/' && s[j]!=':');
  52.  
  53.    /* No dot in the file name */
  54.    s[i] = '.'; /* Overwrite '\0' */
  55.    j = fnmatch(p, s, i+1);
  56.    s[i] = '\0';
  57.    return j;
  58. dot:
  59.    if (/* file name contains extension? */ i-j > 1 ||
  60.       !j || s[j-1]=='\\' || s[j-1]=='/' || s[j-1]==':' || s[j-1]=='.')
  61.       return FALSE;
  62.    return fnmatch(p, s, i-1);
  63. }
  64. #else
  65. int fmatch(p, s)  /* UNIX and others */
  66. register char *p; /* pattern */
  67. register char *s; /* string to compare */
  68. {
  69.    for (; *p && *s; ++p, ++s) {
  70.       if (*p == '*') {
  71.          while (*++p == '*') ;
  72.          while (*s) if (fmatch(p, s++)) return TRUE;
  73.          break;
  74.       } else if (*p != '?') {
  75.          if (*s != *p) return FALSE;
  76.       }
  77.    }
  78.    return !(*p || *s);
  79. }
  80. #endif
  81.  
  82. int mismatch __ARGS__((char*, char*, int));
  83.  
  84. int mismatch(p, s, l)
  85. register char *p, *s; int l;
  86. {
  87.    for (; *p && l; ++p, ++s, l--) {
  88.       if (*p == '*') {
  89.          while (*++p == '*');
  90.          while (l) if (!mismatch(p, s++, l--)) return FALSE;
  91.          break;
  92.       } else if (*p != '?') {
  93.          if (*s != *p) return ERROR;
  94.       }
  95.    }
  96.    return *p || l;
  97. }
  98.